home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Sprocket Framework DR2 / Sprocket Starter / SprocketStarter Code / SampleWindow.cp < prev    next >
Text File  |  1996-06-15  |  9KB  |  431 lines

  1. /*
  2.  
  3.     File:        SampleWindow.cp
  4.     Project:    Sample code for Sprocket Framework 1.1 (DR2), released 6/15/96
  5.     Contains:    A drag manager-aware window designed to hold PICTs
  6.                 Designed to show you how easy it is to subclass TWindow
  7.     To Do:        ?
  8.  
  9.  
  10.     Sprocket Major Contributors:
  11.     ----------------------------
  12.     Dave Falkenburg, producer of Sprocket 1.0
  13.     Bill Hayden,     producer of Sprocket 1.1
  14.     Steve Sisak,     producer of the upcoming Sprocket 2.0
  15.     
  16.     Pete Alexander        Steve Falkenburg    Randy Thelen
  17.     Eric Berdahl        Nitin Ganatra        Chris K. Thomas
  18.     Marshall Clow        Dave Hershey        Leonard Rosenthal
  19.     Tim Craycroft        Dave Mark            Dean Yu
  20.     David denBoer        Gary Powell
  21.     Cameron Esfahani    Jon Summers            Apple Computer, Inc.
  22.         
  23.     Comments, Additions, or Corrections:
  24.     ------------------------------------
  25.     Bill Hayden, Nikol Software <nikol@codewell.com>
  26.  
  27. */
  28.  
  29.  
  30.  
  31. const short            kPictureWindowTemplateID = 2001;
  32. const short            kDefaultPICTResID = 1025;
  33.  
  34.  
  35. #include "SampleWindow.h"
  36. #include "SprocketMacros.h"
  37. #include <ToolUtils.h>
  38.  
  39.  
  40. MenuRef                TSampleWindow::fgMenu;
  41. unsigned long        TSampleWindow::fgWindowTitleCount = 0;
  42.  
  43. extern TMenuBar*    gMenuBar;
  44.  
  45. //    methods
  46.  
  47. TSampleWindow::TSampleWindow()
  48. {
  49.     fDraggedPicHandle = nil;
  50.     fCenter = true;
  51.  
  52.     SetUpStaticMenu();        // CreateWindow will call Activate(), so we must do this first
  53.  
  54.     TSampleWindow::fgWindowTitleCount++;    //    Starts out as zero but we want “Picture Window 1”
  55.     this->CreateWindow();    // kNormalWindow is default. Method inherited from TWindow
  56.                             // TWindow::CreateWindow() calls our MakeNewWindow().
  57.         
  58.     ShowWindow(fWindow);
  59. }
  60.  
  61.  
  62. TSampleWindow::~TSampleWindow()
  63. {
  64. //    No particular cleanup in this version, since our real purpose was
  65. //    to demonstrate the Drag Manager, not to build a picture handling app.
  66. }
  67.  
  68.     
  69. WindowRef TSampleWindow::MakeNewWindow( WindowRef behindWindow )
  70. {
  71.     WindowRef    aWindow;
  72.     Str255        titleString;
  73.     GrafPtr        savedPort;
  74.     
  75.     GetPort(&savedPort);
  76.     
  77.     aWindow = GetNewColorOrBlackAndWhiteWindow( kPictureWindowTemplateID, nil, behindWindow );
  78.     
  79.     if (aWindow)
  80.         {
  81.         GetWTitle(aWindow,titleString);
  82.         if (StrLength(titleString) != 0)
  83.             {
  84.             Str255 numberString;
  85.             
  86.             NumToString( fgWindowTitleCount, numberString );
  87.             BlockMoveData(&numberString[1], &titleString[titleString[0]+1], numberString[0]);
  88.             titleString[0] += numberString[0];
  89.             }
  90.         SetWTitle(aWindow,titleString);
  91.  
  92.         SetPortWindowPort(aWindow);
  93.  
  94.         ShowWindow(aWindow);
  95.         }
  96.     SetPort(savedPort);
  97.  
  98.     return aWindow;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. void TSampleWindow::Draw(void)
  105. {
  106.     PicHandle    pic;
  107.     Rect        r;
  108.     
  109.     SetPortWindowPort(fWindow);
  110.     
  111.     r = GetWindowPort(fWindow)->portRect;
  112.     EraseRect( &r );
  113.     
  114.     if ( fDraggedPicHandle == nil )
  115.         pic = this->LoadDefaultPicture();
  116.     else
  117.         pic = fDraggedPicHandle;
  118.     
  119.     if (fCenter)
  120.         this->CenterPict( pic, &r );
  121.     else
  122.         SetRect(&r, 0, 0, (*pic)->picFrame.right, (*pic)->picFrame.bottom);
  123.         
  124.     DrawPicture( pic, &r );
  125. }
  126.  
  127.  
  128.  
  129.  
  130. void TSampleWindow::Activate( Boolean activating )
  131. {
  132.     if ( activating )
  133.         InsertMenu( fgMenu, 0 );
  134.     else
  135.         DeleteMenu( mPicture );
  136.         
  137.     gMenuBar->Invalidate();
  138. }
  139.  
  140.  
  141.  
  142.  
  143. void TSampleWindow::ClickAndDrag( EventRecord *eventPtr )
  144. {
  145.     OSErr            err;
  146.     DragReference   dragRef;
  147.     RgnHandle       dragRegion, tempRgn;
  148.     Rect            itemBounds;
  149.     Handle            flavorDataHandle;
  150.     
  151.  
  152.     SetPortWindowPort(fWindow);
  153.     
  154.     err = NewDrag( &dragRef );
  155.     if ( err != noErr )
  156.         return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  157.  
  158.     if ( fDraggedPicHandle == nil )
  159.         flavorDataHandle = (Handle)this->LoadDefaultPicture();
  160.     else
  161.         flavorDataHandle = (Handle)fDraggedPicHandle;
  162.  
  163.     HLock( flavorDataHandle );
  164.  
  165.     err = AddDragItemFlavor( dragRef,
  166.                             (ItemReference)fWindow,// no particular reason, could use any number here
  167.                             (FlavorType) 'PICT',
  168.                             (Ptr)*flavorDataHandle,
  169.                             GetHandleSize( (Handle)flavorDataHandle ),
  170.                             (FlavorFlags)0 );
  171.  
  172.     HUnlock( flavorDataHandle );
  173.  
  174.     if ( err != noErr )
  175.         {
  176.         DisposeDrag( dragRef );
  177.         return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  178.         }
  179.     
  180.     itemBounds = this->GetContentsBounds();
  181.     
  182.     GlobalToLocal((Point*) &itemBounds.top);
  183.     GlobalToLocal((Point*) &itemBounds.bottom);
  184.     
  185.     err = SetDragItemBounds( dragRef, (ItemReference)fWindow, &itemBounds );
  186.     if ( err != noErr )
  187.         {
  188.         DisposeDrag( dragRef );
  189.         return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  190.         }
  191.     
  192.     dragRegion = NewRgn();
  193.     RectRgn( dragRegion, &itemBounds );
  194.     tempRgn = NewRgn();
  195.     CopyRgn( dragRegion, tempRgn );
  196.     InsetRgn( tempRgn, 1, 1 );
  197.     DiffRgn( dragRegion, tempRgn, dragRegion );
  198.     DisposeRgn( tempRgn );
  199.     
  200.     err = TrackDrag( dragRef, eventPtr, dragRegion );
  201.     DisposeRgn( dragRegion );
  202.     DisposeDrag( dragRef );
  203.     return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  204. }
  205.  
  206.  
  207.  
  208.  
  209. OSErr TSampleWindow::DragEnterWindow( DragReference dragRef )
  210. {
  211.     fCanAcceptDrag = IsPictFlavorAvailable( dragRef );
  212.     fIsWindowHighlighted = false;
  213.     
  214.     if ( fCanAcceptDrag )
  215.         return noErr;
  216.     else
  217.         return dragNotAcceptedErr;
  218. }
  219.  
  220.  
  221.  
  222.  
  223. OSErr TSampleWindow::DragInWindow( DragReference dragRef )
  224. {
  225.     DragAttributes    attributes;
  226.     RgnHandle        tempRgn;
  227.  
  228.  
  229.     SetPortWindowPort(fWindow);
  230.     
  231.     GetDragAttributes( dragRef, &attributes );
  232.     
  233.     if ( (! fCanAcceptDrag) || (! (attributes & dragHasLeftSenderWindow)) 
  234.         || (attributes & dragInsideSenderWindow) )
  235.         return dragNotAcceptedErr;
  236.     
  237.     if ( this->IsMouseInContentRgn( dragRef ) )
  238.         {
  239.         if ( ! fIsWindowHighlighted )
  240.             {
  241.             tempRgn = NewRgn();
  242.             RectRgn( tempRgn, &GetWindowPort(fWindow)->portRect );
  243.             
  244.             if ( ShowDragHilite( dragRef, tempRgn, true ) == noErr )
  245.                 fIsWindowHighlighted = true;
  246.                 
  247.             DisposeRgn(tempRgn);
  248.             }
  249.         }
  250.         
  251.     return noErr;
  252. }
  253.  
  254.  
  255.  
  256.  
  257. OSErr TSampleWindow::DragLeaveWindow( DragReference dragRef )
  258. {
  259.     if ( fIsWindowHighlighted )
  260.         HideDragHilite( dragRef );
  261.     
  262.     fIsWindowHighlighted = false;
  263.     fCanAcceptDrag = false;
  264.     
  265.     return noErr;
  266. }
  267.  
  268.  
  269.  
  270.  
  271. OSErr TSampleWindow::HandleDrop( DragReference dragRef )
  272. {
  273.     OSErr            err;
  274.     Size            dataSize;
  275.     ItemReference    item;
  276.     FlavorFlags        flags;
  277.     DragAttributes    attributes;
  278.  
  279.     GetDragAttributes( dragRef, &attributes );
  280.     
  281.     if ( attributes & dragInsideSenderWindow )
  282.         return dragNotAcceptedErr;
  283.  
  284.     err = GetDragItemReferenceNumber( dragRef, 1, &item );
  285.     if ( err == noErr )
  286.         err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
  287.  
  288.     if ( err == noErr )
  289.         {
  290.         err = GetFlavorDataSize( dragRef, item, 'PICT', &dataSize);
  291.         if  (err == noErr )
  292.             {
  293.             fDraggedPicHandle = (PicHandle)TempNewHandle( dataSize, &err );
  294.             
  295.             if ( fDraggedPicHandle == nil )
  296.                 fDraggedPicHandle = (PicHandle)NewHandle( dataSize );
  297.  
  298.             if ( fDraggedPicHandle == nil )
  299.                 err = dragNotAcceptedErr;
  300.             else
  301.                 {
  302.                 HLock( (Handle)fDraggedPicHandle );
  303.                 err = GetFlavorData( dragRef, item, 'PICT',
  304.                         *fDraggedPicHandle, &dataSize, 0L );
  305.                 HUnlock( (Handle)fDraggedPicHandle );
  306.  
  307.                 if ( err != noErr)
  308.                     {
  309.                     err = dragNotAcceptedErr;
  310.                     DisposeHandle( (Handle)fDraggedPicHandle );
  311.                     fDraggedPicHandle = nil;
  312.                     }
  313.                 else
  314.                     {
  315.                     SetPortWindowPort( fWindow );
  316.                     InvalRect( &(GetWindowPort(fWindow)->portRect) );
  317.                     }
  318.                 }
  319.             }
  320.         }
  321.     
  322.     return( err );
  323. }
  324.  
  325.  
  326.  
  327.  
  328. PicHandle TSampleWindow::LoadDefaultPicture()
  329. {
  330.     PicHandle    pic;
  331.     
  332.     pic = GetPicture( kDefaultPICTResID );
  333.     
  334.     if ( pic == nil )
  335.         {
  336.         DebugMessage( "\pCould not load PICT resource!" );
  337.         return (PicHandle)nil;
  338.         }
  339.     else
  340.         return( pic );
  341. }
  342.  
  343.  
  344.  
  345.  
  346. void TSampleWindow::CenterPict( PicHandle picture, Rect *destRectPtr )
  347. {
  348.     Rect    windRect, pictRect;
  349.     
  350.     windRect = *destRectPtr;
  351.     pictRect = (**( picture )).picFrame;
  352.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  353.                            windRect.top     - pictRect.top);
  354.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  355.                           (windRect.bottom - pictRect.bottom)/2);
  356.     *destRectPtr = pictRect;
  357. }
  358.  
  359.  
  360.  
  361.  
  362. Boolean TSampleWindow::IsPictFlavorAvailable( DragReference dragRef )
  363. {
  364.     unsigned short    numItems;
  365.     FlavorFlags        flags;
  366.     OSErr            err;
  367.     ItemReference    item;
  368.     
  369.     CountDragItems( dragRef, &numItems );
  370.     
  371.     if ( numItems < 1 )
  372.         return( false );
  373.     
  374.     err = GetDragItemReferenceNumber( dragRef, 1, &item );
  375.     if ( err == noErr )
  376.         err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
  377.     
  378.     return( err == noErr );
  379. }
  380.  
  381.  
  382. void TSampleWindow::SetUpStaticMenu( void )
  383. {
  384.     TSampleWindow::fgMenu = gMenuBar->GetMenuFromCMNU( mPicture );
  385. }
  386.  
  387.  
  388.  
  389.  
  390. /**********************************************************************************/
  391.  
  392.  
  393.  
  394.  
  395. void TSampleWindow::AdjustMenusBeforeMenuSelection(void)
  396. {
  397.     gMenuBar->EnableAndCheckCommand( cCentered, true, fCenter );
  398.     gMenuBar->EnableAndCheckCommand( cUpperLeft, true, !fCenter );
  399. }
  400.  
  401.  
  402.  
  403.  
  404. /**********************************************************************************/
  405.  
  406.  
  407.  
  408.  
  409. Boolean TSampleWindow::DoCommand(CommandID theCommand)
  410. {
  411.     switch(theCommand)
  412.         {
  413.         case    cCentered:
  414.             fCenter = true;
  415.             break;
  416.             
  417.         case    cUpperLeft:
  418.             fCenter = false;
  419.             break;
  420.         
  421.         default:
  422.             return TWindow::DoCommand(theCommand);
  423.             break;
  424.         }
  425.         
  426.     this->Draw();
  427.         
  428.     return true;
  429. }
  430.         
  431.